package com.nanyang.tms.manager.company.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.annotation.SystemMessage;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.company.CompanyPickAddressDao;
import com.nanyang.tms.domain.company.CompanyPickAddressDO;
import com.nanyang.tms.domain.company.CompanyPickAddressQuery;
import com.nanyang.tms.domain.company.CompanyPickAddressVO;
import com.nanyang.tms.manager.company.CompanyPickAddressManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Component("companyPickAddressManager")
public class CompanyPickAddressManagerImpl implements CompanyPickAddressManager {
    @Autowired
    private CompanyPickAddressDao companyPickAddressDao;
    @Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;

    @Override
    public PageBean<CompanyPickAddressVO> listCompanyPickAddress(CompanyPickAddressQuery query) {
        try {
            if (query == null) {
                log.error(TmsCommonEnum.PARAM_IS_NULL.getCode() + TmsCommonEnum.PARAM_IS_NULL.getMessage());
                throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode() ,TmsCommonEnum.PARAM_IS_NULL.getMessage());
            }
            Integer count = companyPickAddressDao.countPickAddress(query);
            PageBean<CompanyPickAddressVO> page = new PageTableBean<CompanyPickAddressVO>(query.getPageIndex(), query.getPageSize());
            if (null != count && count != 0) {
                page.setTotalItem(count);
                int startRow = page.getStartRow();
                if (startRow > 0) {
                    startRow -= 1;
                }
                query.setStartRow(startRow);
                page.addAll(companyPickAddressDao.listPickAddress(query));
            }
            return page;
        } catch (Exception e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }
    @Override
    public void saveCompanyPickAddress(CompanyPickAddressDO companyPickAddressDO) {
        try {
            companyPickAddressDO.setPickCode(TmsSerializeConstant.COMPANYPICKADDRESS_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.COMPANYPICKADDRESS_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY));
            companyPickAddressDao.insert(companyPickAddressDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

    @Override
    @Transactional
    public void updateDefault(CompanyPickAddressDO companyPickAddressDO) {
        try {
            CompanyPickAddressQuery query=new CompanyPickAddressQuery();
            query.setCompanyCode(companyPickAddressDO.getCompanyCode());
            query.setIsDefault(1);
            List<CompanyPickAddressVO> companyPickAddressVOS = companyPickAddressDao.listPickAddress(query);
            for (CompanyPickAddressVO companyPickAddressVO : companyPickAddressVOS) {
                CompanyPickAddressDO pickAddressDO=new CompanyPickAddressDO();
                BeanUtils.copyProperties(companyPickAddressVO,pickAddressDO);
                companyPickAddressDao.cancelDefault(pickAddressDO);
            }
            companyPickAddressDao.updateDefault(companyPickAddressDO);
        } catch (TmsManagerException e) {
            throw TmsExceptionUtil.wrapException(e);
        }
    }

}
